package com.zklh.test;


import javax.swing.tree.TreeNode;

public class Search {
    public static void main(String[] args){
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(4);
        Search a = new Search();
        System.out.println(a.hasPathSum(root, 5));
    }
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null){
            return false;
        }
        return sum(root,  sum);
    }
    int now = 0;
    public boolean sum(TreeNode root, int sum){
        now += root.val;
        if(root.left == null && root.right == null){
            if(now == sum ){return true;}
            else{return false;}
        } else if (root.left == null) {
            return sum(root.right,sum);
        } else if (root.right == null) {
            return sum(root.left,sum);
        }
        return sum(root.left,sum) || sum(root.right,sum);
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
